{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "bfc8dad1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入NumPy库 \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c2173b9",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7285a9ea",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "arr1 = np.array([1, 2, 3, 4])  # 创建一维数组 \n",
    "print(' 创建的数组为： ', arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ddf786ac",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为：\n",
      " [[ 1  2  3  4]\n",
      " [ 4  5  6  7]\n",
      " [ 7  8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "# 创建二维数组\n",
    "arr2 = np.array([\n",
    "    [1, 2, 3, 4],\n",
    "    [4, 5, 6, 7],\n",
    "    [7, 8, 9, 10]\n",
    "])\n",
    "print('创建的数组为：\\n', arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fa528e8c",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重新设置shape 后的arr2 为：\n",
      " [[ 1  2  3]\n",
      " [ 4  4  5]\n",
      " [ 6  7  7]\n",
      " [ 8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-2\n",
    "arr2.shape = 4,3  # 重新设置shape\n",
    "print('重新设置shape 后的arr2 为：\\n', arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e138ac9d",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8e1339eb",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用arange函数创建的数组为：\n",
      " [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-3\n",
    "print('使用arange函数创建的数组为：\\n', np.arange(0,1,0.1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9c163662",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用arange函数创建的数组为：\n",
      " [  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18\n",
      "  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36\n",
      "  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54\n",
      "  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72\n",
      "  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90\n",
      "  91  92  93  94  95  96  97  98  99 100]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-3\n",
    "print('使用arange函数创建的数组为：\\n', np.arange(1,101,1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7a4775e0",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用arange函数创建的数组为：\n",
      " [  1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.\n",
      "  15.  16.  17.  18.  19.  20.  21.  22.  23.  24.  25.  26.  27.  28.\n",
      "  29.  30.  31.  32.  33.  34.  35.  36.  37.  38.  39.  40.  41.  42.\n",
      "  43.  44.  45.  46.  47.  48.  49.  50.  51.  52.  53.  54.  55.  56.\n",
      "  57.  58.  59.  60.  61.  62.  63.  64.  65.  66.  67.  68.  69.  70.\n",
      "  71.  72.  73.  74.  75.  76.  77.  78.  79.  80.  81.  82.  83.  84.\n",
      "  85.  86.  87.  88.  89.  90.  91.  92.  93.  94.  95.  96.  97.  98.\n",
      "  99. 100.]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-3\n",
    "print('使用arange函数创建的数组为：\\n', np.linspace(1,100,100))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0a5f42b7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用logspace函数创建的数组为：\n",
      " [  1.  10. 100.]\n"
     ]
    }
   ],
   "source": [
    "print('使用logspace函数创建的数组为：\\n', np.logspace(0, 2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "beca0166",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用zeros函数创建的数组为：\n",
      " [[0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-6\n",
    "print('使用zeros函数创建的数组为：\\n', np.zeros((2,3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6e5089a7",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用eye函数创建的数组为：\n",
      " [[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-7\n",
    "print('使用eye函数创建的数组为：\\n', np.eye(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6e37070a",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用diag函数创建的数组为：\n",
      " [[1 0 0 0]\n",
      " [0 3 0 0]\n",
      " [0 0 5 0]\n",
      " [0 0 0 7]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-8\n",
    "print('使用diag函数创建的数组为：\\n', np.diag([1,3,5,7],k=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ee16451",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 代码 2-9\n",
    "print('使用ones函数的数组为：\\n', np.ones((5,3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2badc991",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 代码 2-10\n",
    "print('转换结果为：', np.float64(42))  # 整型转换为浮点型\n",
    "print('转换结果为：', np.int8(42.0))  # 浮点型转换为整型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5c561e0f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换结果为： True\n"
     ]
    }
   ],
   "source": [
    "print('转换结果为：', bool(-42))  # 整型转换为布尔型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "52ae84e5",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换结果为： False\n"
     ]
    }
   ],
   "source": [
    "print('转换结果为：', np.bool_(0))  # 整型转换为布尔型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fae06bf",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "print('转换结果为：', np.float(True))  # 布尔型转换为浮点型\n",
    "print('转换结果为：', np.float(False))  # 布尔型转换为浮点型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cf9ac6f",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 数据类型章节"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "10cc825a",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型为：\n",
      " [('name', '<U40'), ('numitems', '<i8'), ('price', '<f8')]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-11\n",
    "df = np.dtype([('name', np.str_, 40), ('numitems', np.int64),\n",
    "    ('price',np.float64)])\n",
    "print('数据类型为：\\n', df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "acbd52af",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型为： <U40\n",
      "数据类型为： <U40\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-12\n",
    "print('数据类型为：', df['name'])\n",
    "print('数据类型为：', np.dtype(df['name']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7974cfa3",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "这里写个标题"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da39a82c",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 随机数章节"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "030310b2",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d8cb4c53",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [0.65992418 0.16500122 0.87212553 0.58572722 0.5464832  0.02191143\n",
      " 0.31680952 0.3551471  0.30762765 0.94750616 0.22414773 0.84376591\n",
      " 0.72637924 0.18218071 0.84554065 0.30403151 0.30741359 0.69762763\n",
      " 0.64635849 0.07218712 0.04673797 0.11201577 0.4625954  0.23952462\n",
      " 0.07550161 0.23684745 0.4837654  0.75342828 0.45109502 0.2145033\n",
      " 0.89155757 0.66242331 0.10317047 0.33847585 0.03149527 0.59494691\n",
      " 0.64044079 0.97072588 0.91375755 0.04359189 0.23387118 0.50722591\n",
      " 0.13417567 0.42478588 0.31137461 0.25519849 0.41941425 0.58727\n",
      " 0.57079567 0.89347634 0.31823386 0.34558944 0.50259036 0.50685004\n",
      " 0.38693335 0.33344025 0.19580097 0.51803077 0.47158399 0.23231871\n",
      " 0.81434684 0.37355662 0.1166134  0.95142093 0.57194074 0.94376996\n",
      " 0.08756903 0.0926506  0.1073909  0.58520665 0.95385175 0.3332744\n",
      " 0.18350042 0.25983657 0.58293329 0.87080596 0.66710866 0.8577368\n",
      " 0.35375246 0.41086267 0.2842253  0.61000039 0.04358557 0.79437827\n",
      " 0.39219624 0.9840432  0.17186017 0.75328785 0.8392276  0.2078176\n",
      " 0.44331371 0.72512044 0.45836653 0.06639729 0.40871785 0.86505274\n",
      " 0.99071857 0.72759988 0.12522198 0.30754186]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-14\n",
    "print('生成的随机数组为：\\n', np.random.random(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "8f5a619f",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[0.73657403 0.59969933 0.8455049  0.96577294 0.34965162]\n",
      " [0.6404426  0.79956947 0.67902641 0.02883087 0.7258193 ]\n",
      " [0.52446315 0.83929056 0.28402225 0.85405176 0.66626862]\n",
      " [0.52174112 0.11953743 0.07305519 0.14683975 0.82319825]\n",
      " [0.51493429 0.72971242 0.14794715 0.55472777 0.57550889]\n",
      " [0.16187918 0.28303193 0.28689057 0.98377428 0.50736768]\n",
      " [0.27681414 0.94276139 0.91186131 0.25674904 0.27491713]\n",
      " [0.90493289 0.56603525 0.54007148 0.19645576 0.56013355]\n",
      " [0.55558067 0.92940655 0.33926081 0.44107565 0.74112108]\n",
      " [0.84530187 0.85552521 0.41187389 0.15733316 0.02000217]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-15\n",
    "print('生成的随机数组为：\\n', np.random.rand(10,5))  #10行5列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "0ffd8d89",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[ 0.03994715  0.24246961 -0.64700728 -1.82121599  0.78227702]\n",
      " [-0.8788161  -1.58781315 -0.45125653 -0.8079752  -0.44333254]\n",
      " [ 0.96265544  0.28214724  0.40918335 -0.87665287  0.55000766]\n",
      " [ 0.86364142 -0.37702259 -0.76547504  0.76223152 -0.2061383 ]\n",
      " [-0.28881737 -0.38497992  0.2002807  -1.39232549  0.55515482]\n",
      " [-0.03006908  0.28142069 -0.6229974   0.69701736  2.52843253]\n",
      " [-1.10212244  1.96050582  0.99111794  0.75595565 -0.3626503 ]\n",
      " [ 1.55581474 -0.29294586 -0.84178746  0.03160247 -0.20511638]\n",
      " [-0.26302202  1.55709873 -0.46169737 -0.40678977  1.08814082]\n",
      " [ 0.59597583  0.57630359  1.77125971  1.25345209 -0.176873  ]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-16\n",
    "print('生成的随机数组为：\\n', np.random.randn(10,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "f781ed2a",
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[4 5 7 9 2]\n",
      " [9 9 4 6 6]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-17\n",
    "print('生成的随机数组为：\\n', np.random.randint(2, 10, size=[2,5]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c19896fa",
   "metadata": {},
   "source": [
    "# 组合分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "43d474b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-27\n",
    "arr = np.arange(16).reshape(4, 4)\n",
    "print('创建的二维数组为：\\n', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5af1ecfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('横向分割为：\\n', np.hsplit(arr, 2))  # hsplit函数横向分割\n",
    "\n",
    "# 代码 2-28\n",
    "print('纵向分割为：\\n', np.vsplit(arr, 2))  # vsplit函数纵向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "ed4bdcf7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "横向分割为：\n",
      " [array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "# 代码 2-29\n",
    "print('横向分割为：\\n', np.split(arr, 2, axis=1))  # split函数横向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0da5b3c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('纵向分割为：\\n', np.split(arr, 2, axis=0))  # split函数纵向分割"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
